/*  Test Massey-Omura code for polynomial basis  */

#include <stdio.h>
#include "bigint.h"
#include "poly.h"
#include "eliptic.h"
#include "protocols.h"

extern FIELD2N poly_prime;
extern void sha_memory();
extern unsigned long random_seed;

main()
{
	EC_PARAMETER	Base;
	FIELD2N			root[3];
	FIELD2N			side1_dcrpt, side2_dcrpt, message;
	POINT			P1, P2, P3, P4;
	BIGINT			prime_order;
	POINT			temp;
	INDEX			error;
	
/*	char string1[MAXSTRING] = "324518553658426701487448656461467"; /*N 109 */ 
	char string1[MAXSTRING] = "324518553658426723114957572335741"; /*N 111  */
/*	char string1[MAXSTRING] = "5192296858534827627896703833467507"; /*N 113  */
/*	char string1[MAXSTRING] = "680564733841876926932320129493409985129";*/ /*N~ 131 */
/*	char string1[MAXSTRING] = "5444517870735015415344659586094410599059"; /*N 134 (g^2 = g+1)	*/
/*	char string1[MAXSTRING] = "19822884620916109459140767798279811163792081";*/ /*N~ 148 GF(16) */
/*	char string1[MAXSTRING] = "91343852333181432387730573045979447452365303319";*/  /* N 158 */
/*	char string1[MAXSTRING] = "5846006549323611672814741753598448348329118574063"; */ /* 163 */ 

	random_seed = 0x1fe9325b;

	if (!irreducible(&poly_prime)) return(0);
	print_field("poly_prime = ", &poly_prime);
	
	if (error = init_poly_math())
	{
		printf("Can't initialize S matrix, row = %d\n", error);
		return(-1);
	}

/*  compute curve order from Koblitz data  */

	ascii_to_bigint(&string1, &prime_order);
	int_to_field( &prime_order, &Base.pnt_order);
	null( &Base.cofactor);
	Base.cofactor.e[NUMWORD] = 8;

	printf("create Base curve and point\n\n");

	Base.crv.form = 0;
	null(&Base.crv.a2);
	poly_gf8(&poly_prime, root);
	copy( &root[1], &Base.crv.a6);
	print_curve("Public curve", &Base.crv);
	
	rand_point(&P1, &Base.crv);
	poly_elptic_mul(&Base.cofactor, &P1, &Base.pnt, &Base.crv);
	print_point("Base point", &Base.pnt);

/*  test Massey Omura protocol  
	create dummy message      */

	random_field( &message);

/*  sender transmits first point  */

	poly_Massey_Omura_send( &Base, &message, &P1, &side1_dcrpt);
	print_point("First point sent", &P1);
	
/*  receiver replies with second point  */

	poly_Massey_Omura_rcv( &Base, &P1, &P2, &side2_dcrpt);
	print_point("second point returned", &P2);

/*  sender decrypts their portion and returns third point  */

	poly_elptic_mul( &side1_dcrpt, &P2, &P3, &Base.crv);
	print_point("third point sent", &P3);

/*  receiver decrypts message  */

	poly_elptic_mul( &side2_dcrpt, &P3, &P4, &Base.crv);
	print_point("message recived", &P4);
	print_field("z ", &message);
	printf("compared to message sent\n");
	

}